package com.jinbiao.quasar.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.jinbiao.quasar.annotate.OperationLog;
import com.jinbiao.quasar.dto.MenuDto;
import com.jinbiao.quasar.model.SysButton;
import com.jinbiao.quasar.model.SysDict;
import com.jinbiao.quasar.model.SysMenu;
import com.jinbiao.quasar.util.AjaxJson;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.EntityQuery;
import org.sagacity.sqltoy.model.Page;

import java.util.List;

@Controller
@OperationLog
public class DictController {
    @Db
    SqlToyLazyDao dao;
    @Mapping("/dict/get-dict-list")
    public AjaxJson getDictList(int page,int pageSize,String sortBy,boolean desc){
        Page<SysDict> pageResult =  dao.findPageEntity(new Page(pageSize,page),SysDict.class, desc?EntityQuery.create().orderByDesc(sortBy):EntityQuery.create().orderBy(sortBy),SysDict.class);
        return AjaxJson.getSuccessData(pageResult);
    }

    @Mapping("/dict/delete-dict-by-id")
    public AjaxJson deleteDictById(SysDict sysDict){
        return AjaxJson.getSuccessData(dao.delete(sysDict));
    }

    @Mapping("/dict/edit-dict")
    public AjaxJson editDict(SysDict sysDict){
        return AjaxJson.getSuccessData(dao.saveOrUpdate(sysDict));
    }

    @Mapping("/dict/add-dict")
    public AjaxJson addDict(SysDict sysDict){
        return AjaxJson.getSuccessData(dao.saveOrUpdate(sysDict));
    }

    @Mapping("/dict/query-dict-by-id")
    public AjaxJson queryDictById(SysDict sysDict){
        List<SysDict> results = dao.findBySql("sysDictList",sysDict);
        if(CollectionUtil.isNotEmpty(results)){
            SysDict dict = results.get(0);
            return AjaxJson.getSuccessData(dict);
        }
        return AjaxJson.getSuccessData(null);
    }

    @Mapping("/public/get-dict-all")
    public AjaxJson getDictAll(SysDict sysDict){
        return AjaxJson.getSuccessData(dao.findBySql("sysDictList",sysDict));
    }
}
